package com.leiting.mail.util;

import com.leiting.mail.util.properties.factory.DefaultMailReaderPropertiesFactory;
import com.leiting.mail.util.properties.factory.MailReaderPropertiesFactory;

import javax.mail.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;


@Deprecated
public class ReadMailUtil {

    public static List<Message> getMailList(String mailType, final String account, final String password, boolean debug, Integer mailnums) throws Exception {
        List<Message> messageList = new ArrayList<Message>();
        Message[] messages = listMail(mailType,account,password,debug,mailnums);
        Collections.addAll(messageList,messages);
        Collections.reverse(messageList);
        return messageList;
    }


    public static List<Message> getMailListByPage(String mailType, final String account, final String password, Integer page,Integer pagenum) throws Exception {
        return getMailListByPage(mailType,account,password,false,page,pagenum);
    }


    public static List<Message> getMailListByPage(String mailType, final String account, final String password, boolean debug, Integer page,Integer pagenum) throws Exception {
        List<Message> messageList = new ArrayList<Message>();
        Folder folder = getFolder(mailType,account,password,debug);
        int total = folder.getMessageCount();
//        System.out.println("total:"+total);
        int begin,end;
        if(null == page || page <= 0){
            page = 1;
        }
        if(null == pagenum || pagenum <= 0){
            pagenum = 50;
        }

        begin = ((page -1) * pagenum ) +1;
        end = page * pagenum;

        begin = total - begin +1;
        end = total - end +1;

        if(begin <= 0){
            return null;
        }

        if (begin > total) {
            begin = total;
        }

        if (end <= 0) {
            end = 1;
        }

        for(int i = begin; i >= end; --i) {
            messageList.add(folder.getMessage(i));
        }

        return messageList;
    }


    public static Message getMail(String mailType, final String account, final String password, boolean debug, Integer index) throws Exception {
        if (null == mailType || mailType.trim().length() == 0) {
            throw new Exception("不受支持的mailType");
        }
        Folder folder = getFolder(mailType,account,password,debug);
        int total = folder.getMessageCount();
        Message message =folder.getMessage(total-index+1);
        return message;
    }


    @Deprecated
    public static Message[] listMail(String mailType,final String account,final String password,boolean debug,Integer mailnums) throws Exception {
        if (null == mailType || mailType.trim().length() == 0) {
            throw new Exception("不受支持的mailType");
        }
        Message [] messages = null;
        Folder folder = getFolder(mailType,account,password,debug);
        messages = getMessagesFromFolder(folder,mailnums);
        return messages;
    }

    public static Message[] listMail(String mailType,final String account,final String password,boolean debug) throws Exception {
        return listMail(mailType,account,password,false,null);
    }

    public static Message[] listMail(String mailType,String account,String password) throws Exception {
        return listMail(mailType,account,password,false);
    }

    public static Folder getFolder(String mailType,final String account,final String password,boolean debug) throws MessagingException {
        MailReaderPropertiesFactory factory = new DefaultMailReaderPropertiesFactory();
        Properties props = null;
        Folder folder = null;
        if (mailType.equalsIgnoreCase(MailConstant.MAIL_TYPE_QQ)) {
            props = factory.getPop3StoreProperties(MailConstant.QQ_POP_MAIL_SERVER);
            Session session = Session.getInstance(props);
            session.setDebug(debug);
            //session.setDebug(true);

            Store store = session.getStore(MailConstant.PROTOCOL_TYPE_POP3);
            store.connect(MailConstant.QQ_POP_MAIL_SERVER, account, password);
            folder = store.getFolder("inbox");
            folder.open(Folder.READ_WRITE);
        }else if(mailType.equalsIgnoreCase(MailConstant.EXMAIL_TYPE_QQ)){
            props = factory.getPop3StoreProperties(MailConstant.QQ_POP_EXMAIL_SERVER);
            Session session = Session.getInstance(props,new Authenticator() {
                //�˷��󷵻��û�������Ķ���
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    PasswordAuthentication pa = new PasswordAuthentication(account, password);
                    return pa;
                }
            });
            session.setDebug(debug);
            Store store = session.getStore();
            store.connect();
            folder = store.getFolder("inbox");
            folder.open(Folder.READ_ONLY);
        }
        return folder;
    }

    public static Message [] getMessagesFromFolder(Folder folder,Integer mailnum) throws MessagingException {
        Message [] messages = null;
        int total = folder.getMessageCount();
        if(null != mailnum && mailnum > 0 && mailnum < total){
            messages = folder.getMessages(total-mailnum+1,total);
        }else {
            messages = folder.getMessages();
        }
        return messages;
    }
}
